package com.qf.service.impl;

import com.qf.entity.Orders;
import com.qf.entity.WebOrder;
import com.qf.exception.BizException;
import com.qf.mapper.OrdersDao;
import com.qf.service.OrdersService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;

@Service
@Transactional
public class OrdersServiceImpl implements OrdersService {
	private final String imageServerLocation = "http://localhost/images/";
    private OrdersDao ordersDao;

    public OrdersServiceImpl(OrdersDao ordersDao) {
        this.ordersDao = ordersDao;
    }

    @Override
    public List<Orders> queryById(int oUserId, int oStatus) throws BizException {
        List<Orders> list=null;
        Orders orders = null;
        try {
            list = (List<Orders>) ordersDao.queryById(oUserId,oStatus);

//            Integer.parseInt("ab");

        }catch (Exception ex) {
            ex.printStackTrace();
            throw new BizException("业务层查询异常: " + ex.getMessage());
        }
        return list;
    }
	@Override
    public List<Orders> QueryByStatus(Integer status, Integer id) {
        return ordersDao.QueryByStatus(0,23);
    }


    @Override
    public List<WebOrder> getOrdersByStatus(Integer status , Integer userId) {

        List<Orders> ordersList = ordersDao.QueryByStatus(status,userId);

        List<WebOrder> webOrderList = new ArrayList<>();

        Map<String, List<Orders>> map = new HashMap<>();
        Map<String, Date> orderTimeMap = new HashMap<>();
        Map<String, BigDecimal> orderMoneyMap = new HashMap<>();

        for (Orders o : ordersList) {
            String orderNo = o.getONo(); //订单编号
            Date createTime = o.getOCreateTime(); //订单的创建日期
            BigDecimal oneMoney = o.getOGoodPrice();//订单单个商品的价格
            BigDecimal oneNum = new BigDecimal(o.getONum());//订单单个商品的数量
            o.setOGoodImg(imageServerLocation + o.getOGoodImg());

            // 获取该订单编号的创建日期
            Date createDate = orderTimeMap.get(orderNo);
            if (null == createDate) {
                orderTimeMap.put(orderNo, createTime);
            }

            // 计算该订单的总金额
            BigDecimal money = orderMoneyMap.get(orderNo);
            if (null == money) {
                orderMoneyMap.put(orderNo, oneMoney.multiply(oneNum));
            } else {
                orderMoneyMap.put(orderNo, (oneMoney.multiply(oneNum)).add(money));
            }
            // 获取该订单编号对应的所有的订单
            List<Orders> orders = map.get(orderNo);
            if (null == orders) {  //该订单编号没有对应的订单
                orders = new ArrayList<>();
                orders.add(o);
                map.put(orderNo, orders);
            } else { //该订单编号有订单了
                orders.add(o);
            }

        }
        map.forEach((k, v) -> {
            WebOrder webOrder = new WebOrder();

            webOrder.setOrderNo(k);
            webOrder.setOrders(v);

            Date createTime = orderTimeMap.get(k);  //订单的创建日期

            webOrder.setCreateTime(orderTimeMap.get(k));

            webOrder.setOrderMoney(orderMoneyMap.get(k));

            webOrderList.add(webOrder);
        });

        return webOrderList;
    }

    @Override
    public List<Orders> ChangeStatus(String no, Integer status) {
        return ordersDao.ChangeStatus(no,status);
    }


    @Override
    public void timeOutOrder() {
        ordersDao.timeoutOrder();
    }

    @Override
    public Integer batchInsert(List<Orders> ordersList) {

        Integer integer = ordersDao.batchInsert(ordersList);

        return integer;
    }
}
